#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UTable.Objects.Controls;
using System.Windows;
using UTable.Objects.Policies;
using System.Reflection;
using System.IO;
using UTable.ObjectManager.Commands;

namespace UTable.Objects
{
    internal class UTabletop2: UObject
    {
        public List<Type> _apps;

        public UTabletop2(): base()
        {
            this.Loaded += new RoutedEventHandler(UTabletop_Loaded);
            PhysicalLayoutPolicy layoutPolicy = new PhysicalLayoutPolicy(this, true);
            layoutPolicy.DefaultLinearDamping = 0.98d;
            layoutPolicy.DefaultAngularDamping = 0.9d;
            layoutPolicy.CollisionPolicy = new TabletopCollisionPolicy();
            // this.LocalObjectManager.LayoutPolicy = layoutPolicy;
            this.OperatingState = ObjectOperatingState.Editing;
        }

        public void FreshTabletopApps(object sender, EventArgs args)
        {

        }

        public void CreateTabletopApp(int index)
        {

        }

        void UTabletop_Loaded(object sender, RoutedEventArgs e)
        {
        }

        void LoadApps()
        {
            String[] files = Directory.GetFiles("Apps");
            Console.WriteLine("Files: " + files.Length);
            foreach (String file in files)
            {
                IObject obj = LoadApp(file);
                if (obj != null)
                {
                    // UTableHelper.PutCommand(new ObjectCommand(this, ObjectCommandType.Insert, obj));
                }
            }
        }

        public delegate void CreateApplicationHandler(String assemblyPath);

        public void CreateApplication(String assemblyPath)
        {
            IObject obj = LoadApp(assemblyPath);
            if (obj != null)
            {
                // UTableHelper.PutCommand(new ObjectCommand(this, ObjectCommandType.Insert, obj));
            }
        }

        public void CloseApps(object sender, EventArgs args)
        {
            List<IObject> _children = new List<IObject>();
            for (int i = 0; i < this.OwnedObjects.Count; i++ )
            {
                _children.Add(this.OwnedObjects[i]);
            }
            foreach (IObject obj in _children)
            {
                // UTableHelper.PutCommand(new ObjectCommand(this, ObjectCommandType.Remove, obj));
            }
        }

        IObject LoadApp(String assemblyPath)
        {
            /*
            Assembly assembly = Assembly.LoadFrom(assemblyPath);
            // Find the object in this assembly
            Type[] types = assembly.GetTypes();
            Type objectType = null;
            foreach (Type type in types)
            {
                if (type.IsSubclassOf(typeof(UObjectEntity)))
                {
                    objectType = type;
                }
            }

            if (objectType == null)
                return null;
            UObjectEntity instance = Activator.CreateInstance(objectType) as UObjectEntity;
            return instance;*/
            throw new NotImplementedException();
        }
    }
}
